package com.frinder.demo2;

import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import scala.Option;

/**
 * Created by frinder on 2017/7/24.
 */
public class RestartActor extends UntypedActor {

    LoggingAdapter logger = Logging.getLogger(getContext().system(), this);

    public enum Msg {
        DONE, RESTART
    }

    @Override
    public void preStart() throws Exception {
        logger.info("{} {}", "preStart", hashCode());
    }

    @Override
    public void postStop() throws Exception {
        logger.info("{} {}", "postStop", hashCode());
    }

    @Override
    public void preRestart(Throwable reason, Option<Object> message) throws Exception {
        logger.info("{} {}", "preRestart", hashCode());
    }

    @Override
    public void postRestart(Throwable reason) throws Exception {
        logger.info("{} {}", "postRestart", hashCode());
    }

    @Override
    public void onReceive(Object o) throws Exception {
        if (o == Msg.DONE) {
            getContext().stop(getSelf());
        } else if (o == Msg.RESTART) {
            ((Object) null).toString();
            logger.error("{}", o);
        } else {
            unhandled(o);
        }
    }
}
